//
//  NewsTableViewController.m
//  j绝版新闻客户端
//
//  Created by weige on 16/5/10.
//  Copyright © 2016年 weige. All rights reserved.
//

#import "NewsTableViewController.h"
#import "BasicTabBatItem.h"
#import "NewsTableViewCell.h"
#import "NewHeaderInit.h"
@interface NewsTableViewController ()
{
    //选中的按钮
    UIButton *selectButton;
    int flag;
    
}

@end

@implementation NewsTableViewController

-(instancetype)initWithStyle:(UITableViewStyle)style
{
    self=[super initWithStyle:style];
    if (self) {
        
        self.tabBarItem=[BasicTabBatItem tabBarItemInit:@"v2_home" andSelect:@"v2_home_r" andTitle:@"新闻"];
        //设置颜色
        [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
        
    }
    return self;
}

-(void)fish
{
    if (self.allNews.count>0) {
        
        [self.tableView reloadData];
    }
    if (flag==1) {
        
        [self.tableView.mj_header endRefreshing];
    }
    if (flag==2
        ) {
        [self.tableView.mj_footer endRefreshing];
        
    }
    flag=0;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    MJRefreshNormalHeader *header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    [header setTitle:@"将要加载数据" forState:MJRefreshStateIdle];
    [header setTitle:@"下拉加载" forState:MJRefreshStatePulling];
    [header setTitle:@"加载中" forState:MJRefreshStateRefreshing];
    [header beginRefreshing];
    self.tableView.mj_header=header;
    self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefesh)];
    

    [self meun];
   // [self loadNews];
  
}


-(void)loadNews
{
    NSString *urlstr=[NSString stringWithFormat:@"http://127.0.0.1/news/getnews.php?fid=%ld&&index=%lu&&count=6",(long)selectButton.tag,(unsigned long)self.allNews.count];
    NSLog(@"%lu",(unsigned long)self.allNews.count);
    NSURL *url=[NSURL URLWithString:urlstr];
    [self loadNewFromURL:url];
    
}
-(void)refresh
{
    flag=1;
    [self clear];
    [self loadNews];
    
    
    
}
-(void)footerRefesh
{
    flag=2;
    [self loadNews];
    
}
-(void) meun
{
    NewHeaderInit *hear=[[NewHeaderInit alloc]initWithFrame:CGRectMake(0, 0, ScreenBounds.size.width, 80)];
   // hear.frame=;
    self.navigationItem.titleView=hear;
    NSArray * titles=[NSArray arrayWithObjects:@"最新",@"国内",@"国际",@"社会",@"公益", nil];
    for (int i=0; i<titles.count; i++) {
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame=CGRectMake(0, 20, 100, 44);
        btn.tag=i+1;
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        if(i==0)
        {
            selectButton=btn;
            
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
        [btn addTarget:self action:@selector(selectBtn:) forControlEvents:UIControlEventTouchUpInside];
        //btn.titleLabel.tintColor=[UIColor blackColor];
        [btn setTitle:titles[i] forState:UIControlStateNormal];
        [hear addBtn:btn];
        
    }

}
-(void)selectBtn:(UIButton *)sender
{
   
    if (sender!=selectButton) {
        
        NewHeaderInit *heade=(NewHeaderInit*)[sender superview];
        NSLog(@"%@",[sender superview]);
        [heade clearColorBtn];
        [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        selectButton=sender;
        //清除数据
        [self clear];
        [self loadNews];
        
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
